//
//  ViewController.m
//  UITabBar
//
//  Created by 王恕 on 15/12/28.
//  Copyright © 2015年 王恕. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITabBarDelegate>

@property (nonatomic,strong) UITabBar *tabBar;
@property (nonatomic,strong) UITabBarItem *firstItem;
@property (nonatomic,strong) UITabBarItem *sencondItem;
@property (nonatomic,strong) UITabBarItem *thirdItem;
@property (nonatomic,strong) UIView *firstView;
@property (nonatomic,strong) UIView *sencondView;
@property (nonatomic,strong) UIView *thirdView;

@end

@implementation ViewController

-(UIView *)firstView{
    if (!_firstView) {
        _firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 375, self.view.bounds.size.height - 50)];
        _firstView.backgroundColor = [UIColor redColor];
        [self.view addSubview:_firstView];
    }
    return _firstView;
}

-(UIView *)sencondView{
    if (!_sencondView) {
        _sencondView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 375, self.view.bounds.size.height - 50)];
        _sencondView.backgroundColor = [UIColor blueColor];
        [self.view addSubview:_sencondView];
    }
    return _sencondView;
}

-(UIView *)thirdView{
    if (!_thirdView) {
        _thirdView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 375, self.view.bounds.size.height - 50)];
        _thirdView.backgroundColor = [UIColor grayColor];
        [self.view addSubview:_thirdView];
    }
    return _thirdView;
}

-(UITabBarItem *)firstItem{
    if (!_firstItem) {
        _firstItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:0];//系统图标创建标签项
    }
    return _firstItem;
}

-(UITabBarItem *)sencondItem{
    if (!_sencondItem) {
        _sencondItem = [[UITabBarItem alloc] initWithTitle:@"百度" image:[UIImage imageNamed:@"1.png"] tag:1];//用用户图标创建标签项
        _sencondItem.badgeValue = @"宏创";//item的标记
        
        
    }
    return _sencondItem;
}

-(UITabBarItem *)thirdItem{
    if (!_thirdItem) {
        _thirdItem = [[UITabBarItem alloc]initWithTitle:@"宏创" image:[UIImage imageNamed:@"1.png"] tag:2];
        
    }
    return _thirdItem;
}

- (UITabBar *)tabBar{
    if (!_tabBar) {
        _tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0,617, 375, 20)];
        //tabBar属性设置
        
        self.tabBar.items = @[self.firstItem,self.sencondItem,self.thirdItem];//设置多个标签项
        
        //tabBar.barStyle = UIBarStyleBlack;
        
        self.tabBar.backgroundColor = [UIColor redColor];//背景色
        
        // tabBar.backgroundImage = [UIImage imageNamed:@"1"];//背景图片
        
        self.tabBar.tintColor = [UIColor blackColor];//选中item的颜色
        
        // tabBar.barTintColor = [UIColor greenColor];//bar的颜色
        
        self.tabBar.translucent = YES;//是否半透明
        
        
        [self.view addSubview:self.tabBar];

    }
    return _tabBar;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

      self.tabBar.delegate = self;//设置代理

    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

    NSLog(@"didSelect");
    switch (item.tag) {
        case 0:
            [self.sencondView removeFromSuperview];
            [self.thirdView removeFromSuperview];
            [self firstView];
            break;
        case 1:
            [self.firstView removeFromSuperview];
            [self.thirdView removeFromSuperview];
            [self sencondView];
            break;
        case 2:
            [self.sencondView removeFromSuperview];
            [self.thirdView removeFromSuperview];
            [self thirdView];
            break;
        default:
            break;
    }

}

- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items{

    NSLog(@"willBegin");

}
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items {


     NSLog(@"didBegin");

}
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed {

     NSLog(@"willEnd");
}

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed{

     NSLog(@"didEnd");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
